/*
 * Interfaz.java
 *
 * Created on 10 de mayo de 2009, 12:35 AM
 */

package presentacion;

import accesodatos.frontera.conectoraacorreo.ConectoraAIMAP;
import accesodatos.frontera.conectoraacorreo.ConectoraAPOP3;
import accesodatos.frontera.conectoraacorreo.conectorafactory.ConectoraFactory;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Store;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import negocio.control.ServiciosDeCorreo;
import negocio.entidades.ListaDeCorreos;
import negocio.entidades.ServidorSMTP;

/**
 *
 * @author  Administrador
 */
public class IGConfiguracionServidorSMTP extends javax.swing.JFrame {

    /** Creates new form IGConfiguracionServidorSMTP */
    public IGConfiguracionServidorSMTP() {
        inicializarListaDeListas();
        inicializarListaDeServidores();
        initComponents();
    }

    private void inicializarListaDeListas() {
        listasModel = new DefaultListModel();
        LinkedList<ListaDeCorreos> listasDeCorreos = ServiciosDeCorreo.consultarListasDeCorreo();

        Iterator<ListaDeCorreos> itListas = listasDeCorreos.iterator();
        while (itListas.hasNext()) {
            ListaDeCorreos listaDeCorreos = itListas.next();
            listasModel.addElement(listaDeCorreos.getNombre());
        }
    }

    private void inicializarListaDeServidores() {
        servidoresModel = new DefaultListModel();
        LinkedList<ServidorSMTP> servidoresIngresados = ServiciosDeCorreo.consultarServidoresSMTP();

        Iterator<ServidorSMTP> itServidores = servidoresIngresados.iterator();
        while (itServidores.hasNext()) {
            ServidorSMTP servidor = itServidores.next();
            servidoresModel.addElement(servidor.getCorreoRemitente());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        guardar = new javax.swing.JButton();
        cancelar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        thost = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        puerto = new javax.swing.JSpinner();
        jPanel3 = new javax.swing.JPanel();
        eliminar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        servidores = new javax.swing.JList();
        jLabel6 = new javax.swing.JLabel();
        tremitente = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        clave = new javax.swing.JPasswordField();
        ssl = new javax.swing.JCheckBox();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        uImap = new javax.swing.JRadioButton();
        uPop = new javax.swing.JRadioButton();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        EHost = new javax.swing.JTextField();
        Essl = new javax.swing.JCheckBox();
        EPuerto = new javax.swing.JSpinner();
        jLabel13 = new javax.swing.JLabel();
        asunto = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        eRemitente = new javax.swing.JTextField();
        eClave = new javax.swing.JPasswordField();
        jLabel15 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listas = new javax.swing.JList();
        eGuardar = new javax.swing.JButton();
        eCancelar = new javax.swing.JButton();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("SERVIDOR SMTP");

        jLabel2.setText("En esta pestaña usted podrá configurar las principales opciones para el envío de correos: ");

        jLabel3.setFont(new java.awt.Font("Tahoma", 2, 11));
        jLabel3.setText("Ingrese los datos del servidor de salida a configurar:");

        guardar.setText("Guardar");
        guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarActionPerformed(evt);
            }
        });

        cancelar.setText("Cancelar");
        cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarActionPerformed(evt);
            }
        });

        jLabel4.setText("Host:");

        jLabel5.setText("Puerto:");

        puerto.setModel(new javax.swing.SpinnerNumberModel(1, 1, 65535, 1));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Servidores SMTP"));

        eliminar.setText("Eliminar...");
        eliminar.setEnabled(false);
        eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarActionPerformed(evt);
            }
        });

        servidores.setModel(servidoresModel);
        servidores.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(servidores);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(eliminar)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(eliminar)
                .addContainerGap())
        );

        jLabel6.setText("Correo Remitente:");

        jLabel7.setText("Contraseña:");

        ssl.setText("Usar SSL");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(clave)
                            .addComponent(tremitente, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(174, 174, 174))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(thost, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(puerto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(ssl))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(thost, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(puerto, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(ssl)))
                .addGap(11, 11, 11)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(tremitente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(clave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addContainerGap())
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel3))
                            .addGap(35, 35, 35)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(guardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelar)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelar)
                    .addComponent(guardar))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Servidor SMTP", jPanel1);

        buttonGroup1.add(uImap);
        uImap.setText("Usar IMAP");

        buttonGroup1.add(uPop);
        uPop.setSelected(true);
        uPop.setText("Usar POP3");

        jLabel11.setText("Host:");

        jLabel12.setText("Puerto:");

        Essl.setText("Usar SSL");

        EPuerto.setModel(new javax.swing.SpinnerNumberModel(1, 1, 65535, 1));

        jLabel13.setText("Asunto para eliminación:");

        jLabel14.setText("Correo:");

        jLabel15.setText("Contraseña:");

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Listas de Correos"));

        listas.setModel(listasModel);
        jScrollPane2.setViewportView(listas);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                .addContainerGap())
        );

        eGuardar.setText("Guardar");
        eGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eGuardarActionPerformed(evt);
            }
        });

        eCancelar.setText("Cancelar");
        eCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel5Layout.createSequentialGroup()
                                        .addComponent(uPop)
                                        .addGap(18, 18, 18)
                                        .addComponent(uImap))
                                    .addGroup(jPanel5Layout.createSequentialGroup()
                                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel11)
                                            .addComponent(jLabel12))
                                        .addGap(28, 28, 28)
                                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(EPuerto, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                                            .addComponent(EHost, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                                            .addComponent(Essl, javax.swing.GroupLayout.Alignment.LEADING))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel14)
                                    .addComponent(jLabel15))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(eClave, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(eRemitente, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addComponent(jLabel13)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(asunto, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                        .addComponent(eGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(eCancelar))))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(uPop)
                            .addComponent(uImap))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(EHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(EPuerto, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Essl))
                    .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(eRemitente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(eClave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(asunto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(eCancelar)
                    .addComponent(eGuardar)))
        );

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel16.setText("SERVIDOR DE ELIMINACIÓN");

        jLabel17.setText("En esta pestaña usted podrá configurar los servidores de eliminación para las listas de correos:");

        jLabel18.setFont(new java.awt.Font("Tahoma", 2, 11));
        jLabel18.setText("Ingrese los datos del servidor de entrada a configurar:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel16, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel17, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jLabel18))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel16)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel17)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel18)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Servidor de Eliminación", jPanel4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarActionPerformed

        int port;
        String host = thost.getText();
        port = (Integer) puerto.getValue();
        System.out.println(port);

        boolean usarSSL;
        if(ssl.isSelected())
            usarSSL = true;
        else
            usarSSL = false;

        String remitente = tremitente.getText();
        char[] contraseña = clave.getPassword();

        //verifcar campos
        boolean datosIncorrectos = false;
        if( ! verificacion.VerificadoraDeURL.verificarURL(host) ){
            JOptionPane.showMessageDialog(this,
                    "La dirección de host ingresada no es correcta,\npor favor verifiquela e intente de nuevo.",
                    "Error al adicionar Servidor",
                    JOptionPane.ERROR_MESSAGE);
            datosIncorrectos = true;
        }

        if( ! verificacion.VerificadoraDeCorreo.verificarCorreo(remitente) ){
            JOptionPane.showMessageDialog(this,
                    "La dirección de correo ingresada no es correcta,\npor favor verifiquela e intente de nuevo.",
                    "Error al adicionar Servidor",
                    JOptionPane.ERROR_MESSAGE);
            datosIncorrectos = true;
        }

        if( ! datosIncorrectos ){
            boolean resultado = negocio.control.ServiciosDeCorreo.GuardarSMTP(host.trim(), port, usarSSL, remitente.trim(), contraseña);
            if( ! resultado ){
                JOptionPane.showMessageDialog(this,
                        "Hubo un error al adicionar el servidor SMTP,\npor favor intente mas tarde.",
                        "Error al adicionar Servidor",
                        JOptionPane.ERROR_MESSAGE);
            }
            this.dispose();
        }
}//GEN-LAST:event_guardarActionPerformed

    private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_cancelarActionPerformed

    private void eGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eGuardarActionPerformed
        //VALIDAR CAMPOS

        int port;
        String host = EHost.getText();
        port = (Integer) EPuerto.getValue();

        boolean usarSSL;
        if(Essl.isSelected()){
            usarSSL = true;
        }else{
            usarSSL = false;
        }

        String remitente = eRemitente.getText();
        char[] contraseña = eClave.getPassword();

        //verifcar campos
        boolean datosIncorrectos = false;
        if( ! verificacion.VerificadoraDeURL.verificarURL(host) ){
            JOptionPane.showMessageDialog(this,
                    "La dirección de host ingresada no es correcta,\npor favor verifiquela e intente de nuevo.",
                    "Error al adicionar Servidor",
                    JOptionPane.ERROR_MESSAGE);
            datosIncorrectos = true;
        }

        if( ! verificacion.VerificadoraDeCorreo.verificarCorreo(remitente) ){
            JOptionPane.showMessageDialog(this,
                    "La dirección de correo ingresada no es correcta,\npor favor verifiquela e intente de nuevo.",
                    "Error al adicionar Servidor",
                    JOptionPane.ERROR_MESSAGE);
            datosIncorrectos = true;
        }

        if( ! datosIncorrectos ){
            
            int tipo;
            if(uImap.isSelected()){
                tipo=ConectoraFactory.IMAP;
                //DEBUG
                ConectoraAIMAP c = new ConectoraAIMAP();
                c.setHost(host);
                c.setPuerto(port);
                c.setUsarSSL(usarSSL);
                c.setUsuario(remitente);
                c.setContrasena(String.valueOf(contraseña));
                try {
                    Store s = c.conectar();
                } catch (NoSuchProviderException ex) {
                    JOptionPane.showMessageDialog(this,
                    "Bien",
                    "Error al adicionar Servidor",
                    JOptionPane.INFORMATION_MESSAGE);
                } catch (MessagingException ex) {
                    JOptionPane.showMessageDialog(this,
                    "No conectó",
                    "Error al adicionar Servidor",
                    JOptionPane.ERROR_MESSAGE);
                }
            }else{
                tipo=ConectoraFactory.POP3;
                //DEBUG
                ConectoraAPOP3 c = new ConectoraAPOP3();
                c.setHost(host);
                c.setPuerto(port);
                c.setUsarSSL(usarSSL);
                c.setUsuario(remitente);
                c.setContrasena(String.valueOf(contraseña));
                System.out.println("host: "+host);
                System.out.println("puerto: "+port);
                System.out.println("ssl: "+usarSSL);
                System.out.println("remitente: "+remitente);
                System.out.println("contraseña: "+String.valueOf(contraseña));
                try {
                    Store s = c.conectar();
                    JOptionPane.showMessageDialog(this,
                    "Bien",
                    "Error al adicionar Servidor",
                    JOptionPane.INFORMATION_MESSAGE);
                } catch (NoSuchProviderException ex) {
                    JOptionPane.showMessageDialog(this,
                    "No conectó"+ex.getMessage(),
                    "Error al adicionar Servidor",
                    JOptionPane.ERROR_MESSAGE);
                } catch (MessagingException ex) {
                    JOptionPane.showMessageDialog(this,
                    "No conectó"+ex.getMessage(),
                    "Error al adicionar Servidor",
                    JOptionPane.ERROR_MESSAGE);
                }
            }
            Object[] listasSeleccionadas = listas.getSelectedValues();
            for (int i = 0; i < listasSeleccionadas.length; i++) {
                String nombreLista = (String) listasSeleccionadas[i];
                ListaDeCorreos lista = ServiciosDeCorreo.buscarListaDeCorreos(nombreLista);
                negocio.control.ServiciosDeCorreo.ingresarServidorDeEliminacion(host, String.valueOf(port), String.valueOf(usarSSL), remitente.trim(), contraseña, tipo, asunto.getText(), lista);
            }

            this.dispose();
        }
}//GEN-LAST:event_eGuardarActionPerformed

    private void eCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_eCancelarActionPerformed

    private void eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_eliminarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new IGConfiguracionServidorSMTP().setVisible(true);
            }
        });
    }
    
    //modelos
    private DefaultListModel servidoresModel;
    private DefaultListModel listasModel;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField EHost;
    private javax.swing.JSpinner EPuerto;
    private javax.swing.JCheckBox Essl;
    private javax.swing.JTextField asunto;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cancelar;
    private javax.swing.JPasswordField clave;
    private javax.swing.JButton eCancelar;
    private javax.swing.JPasswordField eClave;
    private javax.swing.JButton eGuardar;
    private javax.swing.JTextField eRemitente;
    private javax.swing.JButton eliminar;
    private javax.swing.JButton guardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JList listas;
    private javax.swing.JSpinner puerto;
    private javax.swing.JList servidores;
    private javax.swing.JCheckBox ssl;
    private javax.swing.JTextField thost;
    private javax.swing.JTextField tremitente;
    private javax.swing.JRadioButton uImap;
    private javax.swing.JRadioButton uPop;
    // End of variables declaration//GEN-END:variables

}
